#include "router_nanoop.h"

NodeRouterNanoOp::NodeRouterNanoOp(Node *parent, NodeRouterNanoOpParam params) :
    Node(parent)
{
    port_receiver = new ChannelExecOpFabricReceiver(this);
    List<int> tmp_885113744;
    for(int temp_885077216=0; temp_885077216<params.routing_map.size(); temp_885077216+=1)
    {
        tmp_885113744.append(0);
    }
    reg_mask_map = tmp_885113744;
    for(int ii=0; ii<params.routing_map.size(); ii+=1)
    {
        for(int temp_885077104=0; temp_885077104<params.routing_map[ii].size(); temp_885077104+=1)
        {
            int routing_id = params.routing_map[ii][temp_885077104];
            reg_mask_map[ii] |= (1)<<(routing_id);
        }
    }
    List<ChannelExecOpFabricSender *> tmp_885145784;
    for(int temp_885061896=0; temp_885061896<params.routing_map.size(); temp_885061896+=1)
    {
        tmp_885145784.append(new ChannelExecOpFabricSender(this));
    }
    port_senders = tmp_885145784;
    reg_is_routing = false;
    List<bool> tmp_885146568;
    for(int temp_885062680=0; temp_885062680<params.routing_map.size(); temp_885062680+=1)
    {
        tmp_885146568.append(false);
    }
    reg_deploy = tmp_885146568;
}

NodeTaskList NodeRouterNanoOp::tasks() const
{
    NodeTaskList list;
    list.append(static_cast<NodeTask>(&NodeRouterNanoOp::task_receive_nano_op));
    list.append(static_cast<NodeTask>(&NodeRouterNanoOp::task_dispatch_nano_op));
    return list;
}

void NodeRouterNanoOp::task_receive_nano_op()
{
    if((reg_is_routing)||(port_receiver->Bus->is_any_empty()))
    {
        return;
    }
    LaneChannelExecOpFabricBusPack ret_885063632 = port_receiver->Bus->get();
    reg_nano_op = ret_885063632.nano_op;
    for(int ii=0; ii<reg_deploy.size(); ii+=1)
    {
        reg_deploy[ii] = ((reg_nano_op.peer_id)&(reg_mask_map[ii]))>(0);
    }
    reg_is_routing = true;
}

void NodeRouterNanoOp::task_dispatch_nano_op()
{
    if(!(reg_is_routing))
    {
        return;
    }
    reg_is_working = false;
    for(int ii=0; ii<reg_deploy.size(); ii+=1)
    {
        if((reg_deploy[ii])&&(!(port_senders[ii]->Bus->is_any_full())))
        {
            port_senders[ii]->Bus->put(reg_nano_op);
            reg_deploy[ii] = false;
        }
        reg_is_working = (reg_is_working)||(reg_deploy[ii]);
    }
    reg_is_routing = reg_is_working;
}